home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 179 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: ix.netcom.com!netnews
  2. From: tjensen@ix.netcom.com (Ted Jensen)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: qsort help
  5. Date: Wed, 03 Jan 1996 02:26:59 GMT
  6. Organization: Netcom
  7. Message-ID: <4ccpgq$qpl@ixnews8.ix.netcom.com>
  8. References: <4ccio7$7c0@charm.magnus.acs.ohio-state.edu>
  9. Reply-To: tjensen@ix.netcom.com
  10. NNTP-Posting-Host: pax-ca7-05.ix.netcom.com
  11. X-NETCOM-Date: Tue Jan 02  6:26:35 PM PST 1996
  12. X-Newsreader: Forte Free Agent 1.0.82
  13.  
  14. xiaoyi@bmecg.bme.ohio-state.edu (Xiaoyi Wu) wrote:
  15.  
  16. > int (*compar)(int *a, int *b)
  17. > {
  18. >   if (*a < *b) return -1;
  19. >   else if (*a == *b) return 0;
  20. >   else return 1;
  21. > }
  22. >
  23. > int return_moved_contour(int ix, int iy)
  24. > {
  25. >   int i, ac, count=0;
  26. >   int y_coord[10]; /* stores y coordinates whose x == ix */
  27. >
  28. >   for (ac=0; ac<areaindex; ac++)
  29. >     {
  30. >       for (i=0; i<index[ac]; i++)
  31. >     {
  32. >       if (xpos[ac][i] == ix) y_coord[count++] = ypos[ac][i];
  33. >     }
  34. >       if (count == 0) continue; /* not in this contour */
  35. >       else {
  36. >     qsort(y_coord, count, sizeof(int), compar);
  37. >     i = 0;
  38. >     while (i<count/2) {
  39. >       if ((y_coord[i] <= iy) && (y_coord[i+1] >= iy))
  40. >         return ac;
  41. >       else i+=2;
  42. >     }
  43. >       }
  44. >     }
  45. >   return -1; /* cursor not within a contour, no response will be generated */
  46. > }
  47.  
  48. The function prototype for qsort() is:
  49.  
  50.     void qsort(void *base, size_t nelem, size_t width,
  51.                 int(*fcmp)(const void *, const void *));
  52.  
  53. That last parameter is a pointer to a function which returns an
  54. integer and which has two parameters of type const pointer to
  55. void.  Your compare function must be such a function which means
  56. it must have a prototype:
  57.  
  58.     int compar(const void *p1, const void *p2);
  59.  
  60. Then, on entering this function, have to cast these passed
  61. pointers to that type in agreement with where they actually
  62. point.  Since in your case they point to integers in your array,
  63. you might do something like:
  64.  
  65.     int compar(const void *p1, const void *p2)
  66.     {
  67.       int *px1, *px2;
  68.       px1 = (int *)p1;
  69.       px2 = (int *)p2;
  70.  
  71.       and then use px1 and px2 to make the appropriate
  72. comparisons.
  73.  
  74. I didn't go through your main() in detail, so I can't vouch that
  75. making this change will make your program work.  But, it should
  76. work better than it does now!
  77.  
  78. BTW, if you're interested, I have written a tutorial on pointers
  79. and arrays which includes a discussion of function pointers,
  80. particularly as they are used in this kind of circumstance.  You
  81. can get it at one of the following locations:
  82.  
  83. http://oak.oakland.edu:8080/SimTel/msdos/c/ptrtut01.zip
  84.  
  85. ftp.coast.net/SimTel/msdos/c/ptrtut01.zip
  86.  
  87. Hope this helps!
  88.  
  89. Ted Jensen                 Author of PTRTUT01.ZIP
  90. Redwood City, CA      A tutorial on C pointers and Arrays
  91. tjensen@ix.netcom.com    Available via Simtel/msdos/c
  92.  
  93.